package soul.web.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import soul.web.code.Status;

import java.util.Collection;

/**
 * Created by sunzhengjie on 2016/11/21.
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Response {

    private int code;

    private Object msg;

    private Object data;

    @JsonProperty("total_page")
    public long getTotal(){
        if(data instanceof Collection){
            int totalPageNum = ((Collection) data).size()/10;
            return totalPageNum < 1 ? 1 : totalPageNum;
        }
        return -1;
    }

    public Object getData() {
        return data;
    }

    public static Response fail() {
        return Response.ok(Status.fail);
    }

    public static Response fail(Object data) {
        return Response.ok(Status.fail, data);
    }

    public static Response ok() {
        return new Response(Status.Ok.getCode(), Status.Ok.getMsg());
    }

    public static Response ok(Object data) {
        return Response.ok(Status.Ok, data);
    }

    public static Response ok(Status status) {
        return new Response(status.getCode(), status.getMsg());
    }

    public static Response ok(Status status, String msg){
        return new Response(status.getCode(), msg);
    }

    public static Response ok(Status status, Object data) {
        return new Response(status.getCode(), status.getMsg(), data);
    }

    private Response(int code, Object msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private Response(int code, Object msg) {
        this(code, msg, null);
    }

    private Response() {

    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getMsg() {
        return msg;
    }

    public void setMsg(Object msg) {
        this.msg = msg;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
